<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" :plain="true" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="120px">
          <el-form-item label="任务号">
            <el-input v-model="form.robotTaskCode" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="任务异常告警单号">
            <el-input v-model="form.taskWarnCode" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="初次出现故障的时间">
            <el-input v-model="form.startTime" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="错误代码（当前执行任务的机器人唯一标识）">
            <el-input v-model="form.errorCode" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="自定义故障消息">
            <el-input v-model="form.errorMsg" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="额外信息">
            <el-input v-model="form.extra" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="创建时间">
            <el-input v-model="form.createTime" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="创建人">
            <el-input v-model="form.createBy" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="更新人">
            <el-input v-model="form.updateBy" style="width: 250px;" />
          </el-form-item>
          <el-form-item label="更新时间">
            <el-input v-model="form.updateTime" style="width: 250px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column prop="robotTaskCode" label="任务号" />
        <el-table-column prop="taskWarnCode" label="任务异常告警单号" />
        <el-table-column prop="startTime" label="初次出现故障的时间" />
        <el-table-column prop="errorCode" label="错误代码" />
        <el-table-column prop="errorMsg" label="自定义故障消息" />
        <el-table-column prop="extra" label="额外信息" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 'INIT'" type="warning">待处理</el-tag>
            <el-tag v-else type="info">已处理</el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="createBy" label="创建人" />
        <el-table-column prop="updateBy" label="更新人" />
        <el-table-column prop="updateTime" label="更新时间" /> -->
        <el-table-column v-if="checkPer(['admin','wcsAgvTaskError:edit','wcsAgvTaskError:del'])" label="操作" width="200px" align="center">
          <template slot-scope="scope">
            <div style="display: flex;">
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="点击此按钮,将强制取消该任务,是否继续"
                @confirm="cancelOrder(scope.row, scope.$index)"
              >
                <el-button slot="reference" type="warning" size="mini" :disabled="scope.row.status !== 'INIT'" :loading="btn.cancel === scope.$index" style="margin-right: 5px;">强制取消</el-button>
              </el-popconfirm>
              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="取消"
                icon="el-icon-info"
                icon-color="red"
                title="点击此按钮,将强制完成该任务,是否继续"
                @confirm="delOrder(scope.row, scope.$index)"
              >
                <el-button slot="reference" type="danger" size="mini" :disabled="scope.row.status !== 'INIT'" :loading="btn.del === scope.$index" style="margin-right: 5px;">强制完成</el-button>
              </el-popconfirm>
              <!-- <udOperation
                :data="scope.row"
                :permission="permission"
              /> -->
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudWcsAgvTaskError from '@/api/work/AgvErrorMission'
import { cancelOrderAPI, delOrderAPI } from '@/api/work/AgvErrorMission'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
// import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
// import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, robotTaskCode: null, taskWarnCode: null, startTime: null, errorCode: null, errorMsg: null, extra: null, createTime: null, createBy: null, updateBy: null, updateTime: null }
export default {
  name: 'AgvErrorMission',
  components: { pagination, crudOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: 'WcsAgvTaskError', url: 'api/wcsAgvTaskError', idField: 'id', sort: 'id,desc', crudMethod: { ...crudWcsAgvTaskError },
      optShow: {
        download: true,
        del: false,
        edit: false,
        reset: true,
        add: false
      }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'wcsAgvTaskError:add'],
        edit: ['wcsAgvTaskError:edit'],
        del: ['admin', 'wcsAgvTaskError:del']
      },
      rules: {
      },
      btn: {
        cancel: '',
        del: ''
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    async cancelOrder(row, index) {
      try {
        this.btn.cancel = index
        const res = await cancelOrderAPI(row.id)
        if (res.code === 200) {
          this.crud.toQuery()
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.message)
        }
      } finally {
        this.btn.cancel = ''
      }
    },
    async delOrder(row, index) {
      try {
        this.btn.del = index
        const res = await delOrderAPI(row.id)
        if (res.code === 200) {
          this.crud.toQuery()
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.message)
        }
      } finally {
        this.btn.del = ''
      }
    }
  }
}
</script>

<style scoped>

</style>
